unit DiagramFactory;

interface

uses DataContainer, pgchartunit, pgdiagramunit, classes, xmlintf, chartaxisunit, pgseriesunit, apgabstractunit,
   ifmsysutilsunit, ifmgdi, sysutils, pganalysistypesunit, apgabstracttypesunit, ifmmemoryunit, math, ifmactionprotocolunit;

type

   TasacsDiagram = class(PgDiagram)
   private
   protected
      m_DataContainer : TasacsDataContainer;
      function CanDelete : Boolean; override;
      function CanCopy : Boolean; override;
      function CanPaste : Boolean; override;
   public
      constructor Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; Title, SubTitle : string; datacontainer : TasacsDataContainer); reintroduce;
      destructor Destroy; override;
      property DataContainer : TasacsDataContainer read m_DataContainer;
   end; // TasacsDiagram


   ITasacsDiagramParent = interface
   ['{A00CA1D7-0595-415A-B790-CD4B1367C3C7}']
      function GetYearClassNDiagram(iDgr : Integer) : TasacsDiagram;
   end; // ITasacsDiagramParent


   TasacsChart = class(PgChart)
   private
      m_DataContainer : TasacsDataContainer;
   protected
      function CanDelete : Boolean; override;
      function CanCopy : Boolean; override;
      function CanPaste : Boolean; override;
   public
      constructor Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; datacontainer : TasacsDataContainer); reintroduce;
      property DataContainer : TasacsDataContainer read m_DataContainer;
   end; // TasacsChart

   DSChart = class(TasacsChart)
   private
      m_Ds : Integer;
   public
      constructor Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer); reintroduce;
      property DS : Integer read m_DS;
   end; // DSChart

   TasacsMetaSeries = class(PgMetaSeries)
      function CanDelete : Boolean; override;
      function CanView : Boolean; override;
      procedure OnViewActionExecute(Action : PgDataAction; var Handled : Boolean); override;
   end; // TasacsMetaSeries


   TasacsAxisSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
      function GetIntervalName(I : Integer) : string; override;
   end; // TasacsAxisSeries


   YearAxis = class(PgChartAxis)
   protected
      function GetAxisLabel : string; override;
   end; // YearAxis


   AgeAxis = class(PgChartAxis)
   protected
      function GetAxisLabel : string; override;
   end; // AgeAxis


   AgeAxisSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
   end; // AgeAxisSeries


   StdYearAxis = class(YearAxis)
   protected
      function GetNumDimensionElms : Integer; override;
   end; // StdYearAxis


   StdYearAxisSeries = class(TasacsAxisSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // StdYearAxisSeries


   DsYearAxis = class(YearAxis)
   protected
      function GetNumDimensionElms : Integer; override;
   end; // DsYearAxis


   DsYearAxisSeries = class(TasacsAxisSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // DsYearAxisSeries


   DsAgeAxis = class(AgeAxis)
   protected
      function GetNumDimensionElms : Integer; override;
   end; // DsAgeAxis


   DsAgeAxisSeries = class(AgeAxisSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // DsAgeAxisSeries

implementation


/////////////////////
// TasacsDiagram


constructor TasacsDiagram.Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; Title, SubTitle : string; datacontainer : TasacsDataContainer);
begin
   m_DataContainer := DataContainer;
   if Caption = '' then
      Caption := Title;
   inherited Create(AOwner, ItemNode, '', Caption);
   Self.Title := Title;
   Self.ShowTitle := true;
   Self.SubTitle1 := SubTitle;
   Self.ShowSubTitle1 := true;
   Self.SubTitle2 := datacontainer.Model + '    ' +  datacontainer.RunId;
   Self.ShowSubTitle2 := true;
   Self.SetSubTitle2Position(down);
end; // TasacsDiagram.Create



function TasacsDiagram.CanDelete : Boolean;
begin
   Result := false;
end; // TasacsDiagram.CanDelete


function TasacsDiagram.CanCopy : Boolean;
begin
   Result := false;
end; // TasacsDiagram.CanCopy


function TasacsDiagram.CanPaste : Boolean;
begin
   Result := false;
end; // TasacsDiagram.CanPaste


destructor TasacsDiagram.Destroy;
begin
//   m_Diagrams.Free;
   inherited;
end; // TasacsDiagram.Destroy


////////////////////////
// TasacsChart


constructor TasacsChart.Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; datacontainer : TasacsDataContainer);
   const s_TasacsChartTypeName = 'GS_CHART';
//   const s_LFQChartCaption = 'Gear selectivity';
begin
   m_datacontainer := datacontainer;
   inherited Create(AOwner, ItemNode, s_TasacsChartTypeName, Caption, false, true);
end; // TasacsChart.Create


function TasacsChart.CanDelete : Boolean;
begin
   Result := false;
end; // TasacsChart.CanDelete


function TasacsChart.CanCopy : Boolean;
begin
   Result := false;
end; // TasacsChart.CanCopy


function TasacsChart.CanPaste : Boolean;
begin
   Result := false;
end; // TasacsChart.CanPaste


/////////////////
// DSChart


constructor DSChart.Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer);
begin
   m_Ds := _Ds;
   inherited Create(AOwner, nil, DataSourceContainer(datacontainer.Ds[_Ds]).Key, datacontainer);
end; // DSChart.Create


////////////////////////////////
// TasacsMetaSeries


function TasacsMetaSeries.CanDelete : Boolean;
begin
   Result := false;
end; // TasacsMetaSeries.CanDelete


function TasacsMetaSeries.CanView : Boolean;
begin
   Result := true;
end;


procedure TasacsMetaSeries.OnViewActionExecute(Action : PgDataAction; var Handled : Boolean);
begin
   ShowSeries := not ShowSeries;
   OnPropChanged;
end; // TasacsMetaSeries.OnViewActionExecute


////////////////
// YearAxis


function YearAxis.GetAxisLabel : string;
begin
   if DefaultAxisLabel then
      Result := 'Year'
   else
      Result := inherited GetAxisLabel;
end; // YearAxis.GetAxisLabel


////////////////
// TasacsAxisSeries


function TasacsAxisSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   Assert(IsAxisSeries);
   case SeriesVariable of
      svIntervalLow:
         if PrimaryChartAxis.Continuous then
            Result := safestrtofloat(IntervalName[I + MinCategory])
         else
            Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   end; // case
end; // TasacsAxisSeries.GetInternalValue


function TasacsAxisSeries.GetIntervalName(I : Integer) : string;
begin
   Result := inttostr(I + 1);
end; // TasacsAxisSeries.GetIntervalName


////////////////
// AgeAxis


function AgeAxis.GetAxisLabel : string;
begin
   if DefaultAxisLabel then
      Result := 'Age'
   else
      Result := inherited GetAxisLabel;
end; // YearAxis.GetAxisLabel


////////////////
// AgeAxisSeries


function AgeAxisSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   Assert(IsAxisSeries);
   case SeriesVariable of
      svIntervalLow:
         if PrimaryChartAxis.Continuous then
            Result := safestrtofloat(IntervalName[I + MinCategory])
         else
            Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   end; // case
end; // AgeAxisSeries.GetInternalValue


///////////////////
// StdYearAxis


function StdYearAxis.GetNumDimensionElms : Integer;
begin
   with TasacsChart(ChartComp).DataContainer do
      Result := FloatMatrix(StdData[ord(stdN)]).GetSize(1);
end; // StdYearAxis.GetNumDimensionElms


////////////////
// StdYearAxisSeries


function StdYearAxisSeries.GetIntervalName(I : Integer) : string;
var
   Year : Integer;
begin
   // I range : [0..NumDimElms - 1]; // not relative to axis selection.
   Year := I + TasacsChart(PrimaryChartAxis.ChartComp).DataContainer.FirstYear;
   Result := inttostr(Year);
end; // StdYearAxisSeries.GetIntervalName


//////////////////////////////////
// DsYearAxis


function DsYearAxis.GetNumDimensionElms : Integer;
begin
   with TasacsChart(ChartComp), DsChart(ChartComp) do
      Result := FloatMatrix(DataSourceContainer(DataContainer.DS[DS]).DsData[0]).GetSize(1);
end; // DsYearAxis.GetNumDimensionElms


////////////////
// DsYearAxisSeries


function DsYearAxisSeries.GetIntervalName(I : Integer) : string;
var
   Year : Integer;
begin
   // I range : [0..NumDimElms - 1]; // not relative to axis selection.
   with TasacsChart(PrimaryChartAxis.ChartComp), DSChart(PrimaryChartAxis.ChartComp) do
   Year := I + DataSourceContainer(DataContainer.DS[DS]).FirstYear;
   Result := inttostr(Year);
end; // DsYearAxisSeries.GetIntervalName


//////////////////////////////////
// DsAgeAxis


function DsAgeAxis.GetNumDimensionElms : Integer;
begin
   with TasacsChart(ChartComp), DsChart(ChartComp) do
      Result := FloatMatrix(DataSourceContainer(DataContainer.DS[DS]).DsData[0]).GetSize(2);
end; // DsAgeAxis.GetNumDimensionElms


////////////////
// DsAgeAxisSeries


function DsAgeAxisSeries.GetIntervalName(I : Integer) : string;
var
   Age : Integer;
begin
   // I range : [0..NumDimElms - 1]; // not relative to axis selection.
   with TasacsChart(PrimaryChartAxis.ChartComp), DSChart(PrimaryChartAxis.ChartComp) do
      Age := I + DataSourceContainer(DataContainer.DS[DS]).FirstAge;
   Result := inttostr(Age);
end; // DsAgeAxisSeries.GetIntervalName


end.
